package com.weibo.dip.hetest;

import org.apache.commons.lang.exception.ExceptionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/** Created by haisen on 2018/8/3. */
public class AlertUtil {
  private static final String UPDATE_STATE_BY_APPNAME_SQL =
      "update streaming_alert set state =? where app_name =?";

  private static final String SELECT_SQL = "select * from streaming_alert where state=1";

  // private Connection connection = DataBaseUtil.getInstance();

  private static final Logger LOGGER = LoggerFactory.getLogger(AlertUtil.class);

  /**
   * list all alert info.
   *
   * @return not null
   */
  public List<Alert> getAlertInfos() {
    List<Alert> alerts = new ArrayList<>();
    try {
      Connection connection = DataBaseUtil.getInstance();
      PreparedStatement preparedStatement = connection.prepareStatement(SELECT_SQL);
      ResultSet resultSet = preparedStatement.executeQuery();

      while (resultSet.next()) {
        Alert alert = new Alert();
        alert.setId(resultSet.getInt("id"));
        alert.setAppName(resultSet.getString("app_name"));
        alert.setCollection(resultSet.getInt("collection"));
        alert.setAlertAlive(resultSet.getInt("alert_alive"));
        alert.setAlertRepetitive(resultSet.getInt("alert_repetitive"));
        alert.setAlertRepetitiveGroup(resultSet.getString("alert_repetitive_group"));
        alert.setAlertActive(resultSet.getInt("alert_active"));
        alert.setAlertActiveThreshold(resultSet.getInt("alert_active_threshold"));
        alert.setAlertAccumulation(resultSet.getInt("alert_accumulation"));
        alert.setAlertAccumulationThreshold(resultSet.getInt("alert_accumulation_threshold"));
        alert.setAlertRecentlyReceive(resultSet.getInt("alert_recently_receive"));
        alert.setAlertRecentlyReceiveThreshold(
            resultSet.getFloat("alert_recently_receive_threshold"));
        alert.setAlertReceive(resultSet.getInt("alert_receive"));
        alert.setAlertReceiveThreshold(resultSet.getFloat("alert_receive_threshold"));
        alert.setAlertSchedulingDelay(resultSet.getInt("alert_scheduling_delay"));
        alert.setAlertSchedulingDelayThreshold(
            resultSet.getInt("alert_scheduling_delay_threshold"));
        alert.setAlertProcessing(resultSet.getInt("alert_processing"));
        alert.setAlertProcessingThreshold(resultSet.getInt("alert_processing_threshold"));
        alert.setAlertActiveProcessing(resultSet.getInt("alert_active_processing"));
        alert.setAlertActiveProcessingTimeThreshold(
            resultSet.getInt("alert_active_processing_time_threshold"));
        alert.setAlertActiveProcessingNumThreshold(
            resultSet.getInt("alert_active_processing_num_threshold"));
        alert.setAlertInactiveReceivers(resultSet.getInt("alert_inactive_receivers"));
        alert.setAlertInactiveReceiversThreshold(
            resultSet.getInt("alert_inactive_receivers_threshold"));
        alert.setAlertInactiveExecutors(resultSet.getInt("alert_inactive_executors"));
        alert.setAlertInactiveExecutorsThreshold(
            resultSet.getInt("alert_inactive_executors_threshold"));
        alert.setAlertError(resultSet.getInt("alert_error"));
        alert.setAlertErrorThreshold(resultSet.getInt("alert_error_threshold"));
        alert.setAlertInterval(resultSet.getInt("alert_interval"));
        alert.setAlertGroup(resultSet.getString("alert_group"));
        alert.setState(resultSet.getInt("state"));
        alerts.add(alert);
      }
      resultSet.close();
      preparedStatement.close();
      connection.close();
    } catch (SQLException e) {
      LOGGER.error(ExceptionUtils.getFullStackTrace(e));
    }
    return alerts;
  }

  /**
   * update state.
   *
   * @param appName appname
   * @param state state
   */
  public void updateStateByAppName(int state, String appName) {
    try {
      Connection connection = DataBaseUtil.getInstance();
      PreparedStatement preStmt = connection.prepareStatement(UPDATE_STATE_BY_APPNAME_SQL);
      preStmt.setInt(1, state);
      preStmt.setString(2, appName);
      preStmt.executeUpdate();
      preStmt.close();
      connection.close();
    } catch (SQLException e) {
      LOGGER.error(ExceptionUtils.getFullStackTrace(e));
    }
  }
}
